

*------------ 1. Clean the data
*--- 1-1 Google Search Value
import delimited using "data/source/Google Search/Bitcoin_Value.csv", clear
drop if _n == 1| _n == 2
gen currency_id = "bitcoin"
destring v2, replace force
save "data/working/Bitcoin_Value", replace

import delimited using "data/source/Google Search/Bitcoin_Cash_Value.csv", clear
drop if _n == 1| _n == 2
gen currency_id = "bitcoin_cash"
destring v2, replace force
save "data/working/Bitcoin_Cash_Value", replace

import delimited using "data/source/Google Search/Dash_Value.csv", clear
drop if _n == 1| _n == 2
gen currency_id = "dash"
destring v2, replace force
save "data/working/Dash_Value", replace

import delimited using "data/source/Google Search/ethereum_Value.csv", clear
drop if _n == 1| _n == 2
gen currency_id = "ethereum"
destring v2, replace force
save "data/working/ethereum_Value", replace

import delimited using "data/source/Google Search/litecoin_Value.csv", clear
drop if _n == 1| _n == 2
gen currency_id = "litecoin"
destring v2, replace force
save "data/working/litecoin_Value", replace

import delimited using "data/source/Google Search/monero_Value.csv", clear
drop if _n == 1| _n == 2
gen currency_id = "monero"
destring v2, replace force
save "data/working/monero_Value", replace

import delimited using "data/source/Google Search/ripple_Value.csv", clear
drop if _n == 1| _n == 2
gen currency_id = "ripple"
destring v2, replace force
save "data/working/ripple_Value", replace

import delimited using "data/source/Google Search/zcash_Value.csv", clear
drop if _n == 1| _n == 2
gen currency_id = "zcash"
destring v2, replace force
save "data/working/zcash_Value", replace

import delimited using "data/source/Google Search/swftcoin_Value.csv", clear
drop if _n == 1| _n == 2
gen currency_id = "swftcoin"
destring v2, replace force
save "data/working/swftcoin_Value", replace

import delimited using "data/source/Google Search/Bytecoin_Value.csv", clear
drop if _n == 1| _n == 2
gen currency_id = "Bytecoin"
destring v2, replace force
save "data/working/Bytecoin_Value", replace


*--- 1-2 Google Search Hack
import delimited using "data/source/Google Search/Bitcoin_Hack.csv", clear
drop if _n == 1| _n == 2
gen currency_id = "bitcoin"
destring v2, replace force
save "data/working/Bitcoin_Hack", replace

import delimited using "data/source/Google Search/Bitcoin_Cash_Hack.csv", clear
drop if _n == 1| _n == 2
gen currency_id = "bitcoin_cash"
destring v2, replace force
save "data/working/Bitcoin_Cash_Hack", replace

import delimited using "data/source/Google Search/Dash_Hack.csv", clear
drop if _n == 1| _n == 2
gen currency_id = "dash"
destring v2, replace force
save "data/working/Dash_Hack", replace

import delimited using "data/source/Google Search/ethereum_Hack.csv", clear
drop if _n == 1| _n == 2
gen currency_id = "ethereum"
destring v2, replace force
save "data/working/ethereum_Hack", replace

import delimited using "data/source/Google Search/litecoin_Hack.csv", clear
drop if _n == 1| _n == 2
gen currency_id = "litecoin"
destring v2, replace force
save "data/working/litecoin_Hack", replace

import delimited using "data/source/Google Search/ripple_Hack.csv", clear
drop if _n == 1| _n == 2
gen currency_id = "ripple"
destring v2, replace force
save "data/working/ripple_Hack", replace




*--- 1-3 Value combine
use "data/working/swftcoin_Value", clear
append using "data/working/Bytecoin_Value"
collapse (mean) v2, by(v1)
gen currency_id = "ALT"
append using "data/working/Bitcoin_Value"
append using "data/working/Bitcoin_Cash_Value"
append using "data/working/Dash_Value"
append using "data/working/ethereum_Value"
append using "data/working/litecoin_Value"
append using "data/working/monero_Value"
append using "data/working/ripple_Value"
append using "data/working/zcash_Value"

gen date = date(v1,"YMD")
rename v2 google_value
keep date currency_id google_value
order date currency_id google_value

*--- Average of past 28 days
rangestat  (mean) mgoogle_value = google_value, interval(date -28 -1) by(currency_id)
gen dgoogle_value = google_value - mgoogle_value
drop mgoogle_value

encode currency_id, gen(dcurrency_id)
xtset dcurrency_id date
gen l7_dgoogle_value = L7.dgoogle_value
gen l14_dgoogle_value = L14.dgoogle_value
drop dcurrency_id

save "data/working/google_value", replace

*--- 1-4 Value Bitcoin
use "data/working/google_value", clear
keep if currency_id == "bitcoin"
rename google_value google_value_Bitcoin
rename dgoogle_value dgoogle_value_Bitcoin
keep date google_value_Bitcoin dgoogle_value_Bitcoin
order date google_value_Bitcoin dgoogle_value_Bitcoin

tset date
gen l7_dgoogle_value_Bitcoin = L7.dgoogle_value_Bitcoin
gen l14_dgoogle_value_Bitcoin = L14.dgoogle_value_Bitcoin

save "data/working/google_value_Bitcoin", replace


*--- 1-5 Hack combine
use "data/working/Bitcoin_Hack", clear
append using "data/working/Bitcoin_Cash_Hack"
append using "data/working/Dash_Hack"
append using "data/working/ethereum_Hack"
append using "data/working/litecoin_Hack"
append using "data/working/ripple_Hack"


gen date = date(v1,"YMD")
rename v2 google_hack
keep date currency_id google_hack
order date currency_id google_hack

merge 1:1 date currency_id using "data/working/google_Value"
replace google_hack = google_hack / google_value
replace google_hack = 0 if google_hack == .
drop _merge
drop l7_dgoogle_value l14_dgoogle_value

encode currency_id, gen(dcurrency_id)
xtset dcurrency_id date
gen l7_google_hack = L7.google_hack
gen l14_google_hack = L14.google_hack
drop dcurrency_id

save "data/working/google_Hack", replace

*--- 1-6 Hack Bitcoin
use "data/working/Bitcoin_Hack", clear
gen date = date(v1,"YMD")
rename v2 google_hack_Bitcoin
keep date google_hack_Bitcoin
order date google_hack_Bitcoin

merge 1:1 date using "data/working/google_Value_Bitcoin"
replace google_hack_Bitcoin = google_hack_Bitcoin / google_value_Bitcoin
drop _merge
drop l7_dgoogle_value_Bitcoin l14_dgoogle_value_Bitcoin

tset date
gen l7_google_hack_Bitcoin = L7.google_hack_Bitcoin
gen l14_google_hack_Bitcoin = L14.google_hack_Bitcoin

save "data/working/google_Hack_Bitcoin", replace


